package com.zhinong.controller.user;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhinong.service.VoiceRecognitionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class VoiceRecognitionController {

    @Autowired
    private VoiceRecognitionService voiceRecognitionService;

    @PostMapping("/recognize")
    public void recognizeVoice(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = voiceRecognitionService.uploadVoiceFileAndRecognize(request);

        response.setContentType("application/json;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            out.print(new ObjectMapper().writeValueAsString(result));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}